from redis import Redis
class RedisCon:
    def __init__(self):
        self.r = Redis(host='localhost',port=6379)
    #存set
    def insert_set(self,key,val):
        self.r.sadd(key,val)
    #取set
    def find_set(self,key):
        val = self.r.smembers(key)
        if val:
            list =[]
            for i in val:
                list.append(str(i,encoding='utf8'))
            return list
        else:
            return None

    #hash类型添加
    def insert_hash(self,key,params,value):
        self.r.hset(key,params,value)
    #取某个属性的值
    def get_value_hash(self,key,params):
        return self.r.hget(key,params)
    #取所有属性的值
    def get_all_hash(self,key):
        return self.r.hgetall(key)
    #删除某个属性
    def del_hash(self,key,params):
        self.r.hdel(key,params)

    #字符串存取
    def set_str(self,key,value,time=0):
        if time > 0:
            self.r.setex(key,time,value)
        else:
            self.r.set(key,value)

    def get_str(self,key):
        return self.r.get(key)

    #列表存
    def list_push(self,key,val):
        self.r.lpush(key,val)
    #列表存
    def list_rpush(self,key,val):
        self.r.rpush(key,val)
    #列表取
    def list_pop(self,key):
        return self.r.rpop(key)

     #列表取
    def list_lpop(self,key):
        return self.r.lpop(key)
    #获取长度
    def list_len(self,key):
        if self.r.exists(key):
            return self.r.llen(key)
        else:
            return 0

    #获取所有
    def list_getall(self,key,start,end):
        return self.r.lrange(key,start,end)

    #sorted set zset
    def sortedset_add(self,pkey,key,score):
        mapping = {key:score}
        self.r.zadd(pkey,mapping)

    #获取
    def sortedset_getall(self,pkey):
        return self.r.zrange(pkey,0,-1)

    #根据分数的区间获取
    def sortedset_getbyscore(self,pkey,min,max):
        return self.r.zrangebyscore(pkey,min,max)

    #删除
    def sortedset_delete(self,pkey,key):
        self.r.zrem(pkey,key)

# import json
# import time
r = RedisCon()
# key = "logoutlist"
# r.sortedset_add(key,str('abc'),int(time.time()))
# r.sortedset_add(key,str('aaa'),int(time.time()))
# res = r.sortedset_getall(key)

# flag = False
# for i in res:
#     if  "abc" == i.decode():
#         flag = True
# if flag == True:
#     print("logout")
# else:
#     print("12343")
# r.sortedset_add("logoutlist","adsfsdf",time.time())
# key = 'interface1'
# list=['/abc','/user']
# # rs.insert_set(key,list)
# mes = rs.find_set(key)
# for i in mes:
#     if '/abcwerw3r2' == str(i,encoding='utf8'):
#         print(1)


